# import cv2
# import numpy as np
#
# # 读取图像
# image_path = "2.png"  # 替换为你的图片路径
# img = cv2.imread(image_path)
#
# # 确保图像为灰度图像
# if len(img.shape) == 3:
#     img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#
# # 定义对比度增强因子
# contrast_factor = 1.5
#
# # 使用OpenCV的convertScaleAbs函数进行线性变换
# # 这里，alpha参数决定增益，beta参数决定偏置
# enhanced_img = cv2.convertScaleAbs(img, alpha=contrast_factor, beta=0)
#
# # 显示原图和增强后的图像
# cv2.imshow("Original Image", img)
# cv2.imshow("Enhanced Image", enhanced_img)
#
# # 等待用户按键，按任意键关闭窗口
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#
# import cv2
# import numpy as np
#
# # 读取图像
# image = cv2.imread('2.png', 0)  # 替换为你的图片路径，确保是灰度图像
#
# # 1. 简单均值模板（每个像素的平均值）
# template = np.ones((3, 3)) / 9
# smoothed_img_mean = cv2.filter2D(image, -1, template)
#
# # 2. 邻域均值模板（3x3邻域的平均值）
# template = np.ones((3, 3)) / 9
# smoothed_img_neighborhood = cv2.filter2D(image, -1, template)
#
# # 3. 高斯模板（使用OpenCV内置的高斯核）
# kernel_size = 5
# sigma = 1
# gaussian_kernel = cv2.getGaussianKernel(kernel_size, sigma)
# smoothed_img_gaussian = cv2.filter2D(image, -1, gaussian_kernel)
#
# # 显示原始图像和处理后的图像
# cv2.imshow("Original Image", image)
# cv2.imshow("Mean Smoothed", smoothed_img_mean)
# cv2.imshow("Neighborhood Smoothed", smoothed_img_neighborhood)
# cv2.imshow("Gaussian Smoothed", smoothed_img_gaussian)
#
# # 等待用户按键，按任意键关闭窗口
# cv2.waitKey(0)
# cv2.destroyAllWindows()


# from scipy.signal import butter, lfilter, freqz
# import matplotlib.pyplot as plt
# import numpy as np
# import cv2
#
# # 读取图像并转换为灰度
# image = cv2.imread('2.png', 0)  # 替换为你的图片路径
# image = cv2.resize(image, (100, 100))  # 为了方便展示，这里先缩放图像
#
# # 设定滤波器参数
# fs = 1.0  # 假设图像频率范围为1 Hz
# cutoff_freq = 0.2  # 选择滤波器截止频率
# order = 4  # 滤波器阶数
#
# # Butterworth滤波器
# b, a = butter(order, cutoff_freq, btype='low')
# filtered_butterworth = lfilter(b, a, image)
#
# # 高斯滤波器
# gaussian_kernel = cv2.getGaussianKernel(5, 1)
# filtered_gaussian = cv2.filter2D(image, -1, gaussian_kernel)
#
# # 指数滤波器
# def exponential_filter(image, alpha=0.5):
#     return (1 - alpha) * image + alpha * np.mean(image)
# filtered_exponential = exponential_filter(image)
#
# # 梯形滤波器
# def trapezoidal_filter(image, alpha=0.5):
#     return (1 - alpha) * image + alpha * (image[0] + image[-1]) / 2
# filtered_trapezoidal = trapezoidal_filter(image)
#
# # 绘制频谱图
# f, ax = plt.subplots(2, 2, figsize=(8, 8))
# ax[0, 0].imshow(image, cmap='gray')
# ax[0, 0].set_title('Original')
#
# ax[0, 1].imshow(filtered_butterworth, cmap='gray')
# ax[0, 1].set_title('Butterworth')
#
# ax[1, 0].imshow(filtered_gaussian, cmap='gray')
# ax[1, 0].set_title('Gaussian')
#
# ax[1, 1].imshow(filtered_exponential, cmap='gray')
# ax[1, 1].set_title('Exponential')
#
# for row in ax:
#     for axi in row:
#         axi.set_xticks([])
#         axi.set_yticks([])
#
# plt.tight_layout()
# plt.show()

# import cv2
# import numpy as np
#
# # 读取图像
# image_path = '2.png'  # 替换为你的图片路径
# image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
#
# # 对数变换
# def log_transform(image):
#     # 对于负值，我们先将其转换为正数，因为对数函数只接受正数
#     image[image < 0] = 0
#     # 对数变换
#     transformed_image = np.log(image + 1)
#     return transformed_image
#
# # 应用对数变换
# transformed_image = log_transform(image)
#
# # 显示原图像和变换后的图像
# cv2.imshow("Original Image", image)
# cv2.imshow("Logarithmic Transformation", transformed_image)
#
# # 等待用户按键，按任意键关闭窗口
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#
# import cv2
# import numpy as np
#
# # 读取图像
# img = cv2.imread('sun.jpg', cv2.IMREAD_GRAYSCALE)
#
# # 对数变换函数
# def log_transform(img, c):
#     img_log = c * np.log1p(img)
#     img_log = np.uint8(img_log)
#     return img_log
#
# # 设置常数c
# c = 1
#
# # 对图像进行对数变换
# img_log = log_transform(img, c)
#
# # 显示原始图像和对数变换后的图像
# cv2.imshow('Original Image', img)
# cv2.imshow('Log Transform Image', img_log)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#
# import cv2
# import numpy as np
# import matplotlib.pyplot as plt
#
# # 读取图像
# img = cv2.imread('2.png', cv2.IMREAD_GRAYSCALE)
#
#
# # 中心化频谱
# def center_spectrum(img):
#     fshift = np.fft.fftshift(np.fft.fft2(img))
#     magnitude_spectrum = 20 * np.log(np.abs(fshift))
#     return magnitude_spectrum
#
#
# # 生成低通滤波器
# def low_pass_filter(filter_type, rows, cols, D0, n):
#     u, v = np.meshgrid(np.arange(-cols // 2, cols // 2), np.arange(-rows // 2, rows // 2))
#     D = np.sqrt(u ** 2 + v ** 2)
#
#     if filter_type == 'butterworth':
#         H = 1 / (1 + (D / D0) ** (2 * n))
#     elif filter_type == 'gaussian':
#         H = np.exp(-D ** 2 / (2 * D0 ** 2))
#     elif filter_type == 'exponential':
#         H = np.exp(-D / D0)
#     elif filter_type == 'trapezoidal':
#         H = np.ones((rows, cols))
#         H[D > D0] = 0
#
#     return H
#
#
# # 应用滤波器
# def apply_filter(img, H):
#     fshift = np.fft.fftshift(np.fft.fft2(img))
#     fshift_filtered = fshift * H
#     img_filtered = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift_filtered)))
#     return img_filtered
#
#
# # 参数设置
# rows, cols = img.shape
# D0 = 50
# n = 2
#
# # 不同类型的低通滤波器
# filter_types = ['butterworth', 'gaussian', 'exponential', 'trapezoidal']
# filtered_images = []
#
# for filter_type in filter_types:
#     H = low_pass_filter(filter_type, rows, cols, D0, n)
#     img_filtered = apply_filter(img, H)
#     filtered_images.append(img_filtered)
#
# # 绘制处理前后图像中心化频谱的差异
# plt.figure(figsize=(12, 8))
# for i in range(len(filter_types)):
#     plt.subplot(2, len(filter_types), i + 1)
#     plt.imshow(center_spectrum(img), cmap='gray')
#     plt.title('Original Spectrum')
#
#     plt.subplot(2, len(filter_types), i + len(filter_types) + 1)
#     plt.imshow(center_spectrum(filtered_images[i]), cmap='gray')
#     plt.title(filter_types[i].capitalize() + ' Filtered Spectrum')
#
# plt.tight_layout()
# plt.show()


